import { createI18n as base, useI18n } from "vue-i18n";
import langTemplate from "./langTemplate.json";
import { ys } from "ys-ts";

export type MessageSchema = typeof langTemplate;

export async function createI18n(locals: string[]) {
  const messages: Record<any, any> = {
    langTemplate: langTemplate,
  };
  const axios = ys.getAxios();

  for (const locale of locals) {
    await axios
      .get<MessageSchema>(`/locales/${locale}.json`)
      .then((res) => {
        messages[locale] = res.data;
      })
      .catch((err) => {
        console.error("[plugin/i18n]", err);
      });
  }

  return base<{ messages: MessageSchema }, "langTemplate", false>({
    legacy: false,
    locale: locals[0],
    messages: {
      langTemplate: langTemplate,
      ...messages,
    },
  });
}
